getAjaxReq('location/listByToken', {},
    function (res) {
        if (res.code == 1) {
            initTree(res.data);
        }
    }
);
var locationId = null;
var parentId = null;

function initTree(data) {
    $('#jstree_div').jstree({
        core: {data: data, themes: {stripes: true}},
        plugins: ["wholerow", "state"]
    });
    $('#jstree_div').bind("select_node.jstree", function (e, obj) {
        var currentNode = obj.node;
        var original = currentNode.original
        locationId = currentNode.id
        parentId = currentNode.id
        $.ajax({
            url: "location/click",
            data: {locationId: locationId},
            success: function (res) {
                if (res.code != 1 || original.mobilePay != 1) {
                    $("#addCode").hide();
                } else
                    $("#addCode").show();
            }
        })
        var parents = currentNode.parents
        var parentName = ''
        if (parents) {
            var length = parents.length
            if (length > 1) {
                for (var i = 0; i < length; i++) {
                    var node = $('#jstree_div').jstree("get_node", parents[length - i - 1])
                    var text = node.text
                    if (!isEmpty(text)) {
                        if (i == 1) text = text
                        else text = ' - ' + text
                        parentName = parentName + text
                    }
                }
            } else {
                parentName = "无"
            }
        }

        $("#latitude").val(original.latitude)
        $("#longitude").val(original.longitude)
        layui.use(["form"], function () {
            var form = layui.form;
            $("#mobilePay").val(original.mobilePay)
            form.render()
        })

        $("#locationId").val(locationId)
        $("#locationName").val(currentNode.text)
        $("#locationName1").val(parentName)
        if (parents.length == 1) {
            $("#locationName2").val(currentNode.text)
            $("#locationName3").val(currentNode.text)
        } else {
            $("#locationName2").val(parentName + ' - ' + currentNode.text)
            $("#locationName3").val(parentName + ' - ' + currentNode.text)
        }

    });
}

layui.use(["form"], function () {
    var form = layui.form;
    form.verify({
        int_number: [int_number, '地址起始值必须输入整数!']
    })
    form.on('submit(form1Submit)', function (obj) {
        var data = obj.field
        if (locationId == null)
            return layer.msg("请选中上级地址！")
        if (isEmpty(data.locationName))
            return layer.msg("请输入地址名称！")
        data.locationId = locationId
        postAjaxReq("location/edit", data, function (res) {
            if (res.code) {
                layer.msg("操作成功")
                window.location.reload()
            } else
                layer.msg(res.msg)
        })
    })
    form.on('submit(form2Submit)', function (obj) {
        var data = obj.field
        if (locationId == null)
            return layer.msg("请选择上级地址！")
        if (isEmpty(data.locationName))
            return layer.msg("请输入地址名称！")
        data.parentId = parentId
        postAjaxReq("location/save", data, function (res) {
            if (res.code) {
                layer.msg("操作成功")
                window.setTimeout(function () {
                    window.location.reload()
                }, 500);
            } else
                layer.msg(res.msg)
        })
    })
    form.on('submit(form3Submit)', function (obj) {
        var data = obj.field
        if (locationId == null)
            return layer.msg("请选择上级地址！")
        if (isEmpty(data.start))
            return layer.msg("请输入地址名称！")
        data.parentId = parentId
        postAjaxReq("location/save/batch", data, function (res) {
            if (res.code) {
                layer.msg("操作成功")
                window.setTimeout(function () {
                    window.location.reload()
                }, 500);
            } else
                layer.msg(res.msg)
        })
    })
    $("#delete").on("click", function () {
        confirm("确认要删除吗？", function () {
            postAjaxReq("location/delete", {locationId: locationId}, function (res) {
                if (res.code) {
                    layer.msg("操作成功")
                    window.location.reload()
                } else
                    layer.msg(res.msg)
            })
        })

    })

})
